#include "delegate.h"
#include <common/constants.h>

#include <QTableView>
#include <QComboBox>
#include <QDateEdit>
#include <QSqlDatabase>
#include <QSqlTableModel>
#include <QSqlQuery>
#include <QSqlError>

#include <QDebug>

using namespace Constants;
using namespace Delegates;

ComboDelegate::ComboDelegate(QObject * parent)
{
     Q_UNUSED(parent);
}

ComboDelegate::~ComboDelegate(){}

QSize ComboDelegate::sizeHint(const QStyleOptionViewItem &  option ,
                              const QModelIndex & index) const
{
    Q_UNUSED(option);
    Q_UNUSED(index);
    return QSize(40,50);
}

QWidget *ComboDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                           const QModelIndex &index) const
{
   if ( index.column() == GENDER )
    {
   	  QComboBox * combo = new QComboBox(parent);
    	  return combo;
        }
   return QStyledItemDelegate::createEditor(parent, option, index);
}

void ComboDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
     QComboBox * combo = static_cast<QComboBox*>(editor);
     combo->setEditable(false);
     QString gender = index.model()->data(index,Qt::DisplayRole).toString();
     QStringList genderlist;
     if (gender == "M")
     {
           genderlist << "M" << "F";
         }
     else
     {
         genderlist << "F" << "M";
         }     
     combo->addItems(genderlist);
}
 
void ComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                      const QModelIndex &index) const
{
     QComboBox * combo = static_cast<QComboBox*>(editor);
     QString text = combo->currentText();
     model->setData(index, text , Qt::EditRole);
}

 //////////////////////////////////////////////////////////
 //////////////DATE EDIT//////////////
 //////////////////////////////////////////
 
 DateDelegate::DateDelegate(QObject * parent)
 {
     Q_UNUSED(parent);
 }

DateDelegate::~DateDelegate(){}

QSize DateDelegate::sizeHint(const QStyleOptionViewItem &  option ,
                              const QModelIndex & index) const
{
    Q_UNUSED(option);
    Q_UNUSED(index);
    return QSize(40,25);
}

QWidget *DateDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                           const QModelIndex &index) const
{
    Q_UNUSED(option);
    Q_UNUSED(index);
    QDateEdit * dateEdit = new QDateEdit(parent);
    dateEdit->setDisplayFormat("yyyy-MM-dd");
    return dateEdit;
}

 void DateDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
 {
     QDateEdit * dateEdit = static_cast<QDateEdit*>(editor);
     QDate date;
     date = index.model()->data(index,Qt::DisplayRole).toDate();
     dateEdit->setDate(date);
}
 
 void DateDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                      const QModelIndex &index) const
 {
     QDateEdit * dateEdit = static_cast<QDateEdit*>(editor);
     QString value = dateEdit->date().toString("yyyy-MM-dd");
     model->setData(index, value, Qt::EditRole);
 }
 

